﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
//updated file 30 12:05
namespace CTEA_INDIA
{
    public partial class Lead_Generator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;
            if (!IsPostBack)
            {
                ddlLoc.Style.Add("display", "none");

                BAL.Customers.PopulateLocation(ref ddlLocation);
                BAL.Customers.PopulateLoc(ref ddlLoc);
                BAL.Customers.PopulateLoc(ref cmbEdtLocation);
                populateLeadGeneration();
            }
            if (Session["UserID"] == null)
            {
                Response.Redirect("Default.aspx");
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Name = txtName.Text;
            string Contact = txtContactNumber.Text;
            string Location = ddlLocation.SelectedValue;
            string Email = txtEmail.Text;
            string InterestedField = cmbInterestedField.Text;
            string EnquiryType = cmbEnquiryType.Text;
            string FollowUp = "aaa";
            string Feedback = txtFeedback.Text;
            string Status = cmbStatus.Text;
            string UnSolvedReason = cmbUnsolvedReason.Text;
            string Transfer = cmbTransfer.Text;

            string RefName = txtRefName.Text;
            string RefContact = txtRefContact.Text;
            string BGC = txtHowBGC.Text;

            BAL.Customers.Lead_Generator(Name, Contact, Location, Email, InterestedField, EnquiryType, FollowUp,
                Feedback, Status, UnSolvedReason, Transfer, RefName, RefContact , BGC);

            spnMsgs.Visible = true;
            spnText.InnerHtml = "Your details have been submitted.";

            populateLeadGeneration();

            txtName.Text = "";
            txtContactNumber.Text = "";
            ddlLocation.SelectedIndex = 0;
            txtEmail.Text = "";
            cmbInterestedField.SelectedIndex = 0;
            cmbEnquiryType.SelectedIndex = 0;

            txtFeedback.Text = "";
            cmbStatus.SelectedIndex = 0;
            cmbUnsolvedReason.SelectedIndex = 0;
            cmbTransfer.SelectedIndex = 0;
            txtRefName.Text = "";
            txtRefContact.Text = "";
            txtHowBGC.Text = "";

        }

        public void populateLeadGeneration()
        {

            DataSet ds = BAL.Customers.viewAllLead();
            gdLeadGeneration.DataSource = ds;
            gdLeadGeneration.DataBind();

        }

        protected void gdLeadGeneration_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdLeadGeneration.PageIndex = e.NewPageIndex;
            populateLeadGeneration();
        }

        protected void gdLeadGeneration_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lEdit = (LinkButton)e.Row.FindControl("lnkEdit");
                Button update = (Button)e.Row.FindControl("btnUpdate");
                LinkButton lDelete = (LinkButton)e.Row.FindControl("lnkDelete");
                lDelete.CommandArgument = e.Row.RowIndex.ToString();
            }

        }

        protected void lnkView_Click(object sender, EventArgs e)
        {
            LinkButton lnkVw = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkVw.Parent.Parent;
            // int locationID = Convert.ToInt32(ddlLocation.SelectedValue);


            int Cid = Convert.ToInt32(gdLeadGeneration.DataKeys[row.RowIndex]["LeadId"]);


            string[] Details = BAL.Customers.GetLeadById(Cid);

            lblName.Text = Details[1];
            lblMob.Text = Details[2];
            lblLocation.Text = Details[3];
            lblEmail.Text = Details[4];
            lblInterestedField.Text = Details[5];
            lblEnquiryType.Text = Details[6];
            lblFollowup.Text = Details[7];
            lblFeedback.Text = Details[8];
            lblStatus.Text = Details[9];
            lblTransfer.Text = Details[11];
            lblRefName.Text = Details[12];
            lblRefContact.Text = Details[13];
            lblViewDate.Text = DateTime.Parse( Details[15]).ToShortDateString();
            lblViewBGC.Text = Details[17];

            lnkView_ModalPopupExtender.Show();

            //document.getElementById("totalDaystd").innerHTML = "<asp:TextBox ID=\"txtDaysText\" runat=\"server\" ReadOnly=\"True\" Text=\""+ days +"\"></asp:TextBox>";

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int Type = Convert.ToInt32(ddlSearch.SelectedValue);
            string SearchTxt = "" ;
            
            if (Type == 0)
            {
                populateLeadGeneration();
            }
            else if (Type == 2)
            {
                SearchTxt = ddlLoc.Text;
                DataSet ds = BAL.Customers.SearchLead(Type, SearchTxt);
                gdLeadGeneration.DataSource = ds;
                gdLeadGeneration.DataBind();
            }
            else if (Type == 3 || Type == 1)
            {
                SearchTxt = txtSearchText.Text;
                DataSet ds = BAL.Customers.SearchLead(Type, SearchTxt);
                gdLeadGeneration.DataSource = ds;
                gdLeadGeneration.DataBind();
            }
            


        }

        protected void lnkEdit_Click(object sender, EventArgs e)
        {
            LinkButton lnkEdit = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkEdit.Parent.Parent;

            int Cid = Convert.ToInt32(gdLeadGeneration.DataKeys[row.RowIndex]["LeadId"]);
            string[] Details = BAL.Customers.GetLeadById(Cid);
            ViewState["LeadId"] = Cid.ToString();
            string[] intArr = { "Acting", "Dancing", "Modelling", "Investment", "ListItem" };

            txtEdtName.Text = Details[1];
            txtEdtContNo.Text = Details[2];
            cmbEdtLocation.SelectedValue = Details[3].ToString();
            txtEdtEmail.Text = Details[4];
            txtEdtHowBGC.Text = Details[17];
            for (int i = 0; i <= 4; i++)
            {
                if (Details[5].ToString().Equals(intArr[i]))
                {
                    cmbEdtInterested.SelectedIndex = i;
                    break;
                }
            }

            string[] enqArr = { "Call", "Visited", "Email" };

            for (int i = 0; i <= 2; i++)
            {
                if (Details[6].ToString().Equals(enqArr[i]))
                {
                    cmbEdtEnquiryType.SelectedIndex = i;
                    break;
                }
            }


            string[] stsArr = { "Pending", "Success", "Unsolved" };

            for (int i = 0; i <= 2; i++)
            {
                if (Details[9].ToString().Equals(stsArr[i]))
                {
                    cmbEdtStatus.SelectedIndex = i;
                    break;
                }
            }

            string[] traArr = { "Acting", "Dancing", "Modelling", "Investment", "Other" };

            for (int i = 0; i <= 4; i++)
            {
                if (Details[11].ToString().Equals(traArr[i]))
                {
                    cmbEdtTransfer.SelectedIndex = i;
                    break;
                }
            }



            txtEdtFeedback.Text = Details[8];
            txtEdtRefComName.Text = Details[12];
            txtEdtRefContact.Text = Details[13];

            LnkEditPopUp.Show();


        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            LinkButton lnkEdit = sender as LinkButton;
            GridViewRow row = (GridViewRow)lnkEdit.Parent.Parent;

            int Cid = Convert.ToInt32(gdLeadGeneration.DataKeys[row.RowIndex]["LeadId"]);
            BAL.Customers.DeleteLeadById(Cid);
            populateLeadGeneration();
        }

        protected void update(object sender, EventArgs e)
        {

            string Id = ViewState["LeadId"].ToString();
            string Name = txtEdtName.Text;
            string Contact = txtEdtContNo.Text;
            string Location = cmbEdtLocation.Text;
            string Email = txtEdtEmail.Text;

            string InterestedField = cmbEdtInterested.Text;
            string EnquiryType = cmbEdtEnquiryType.Text;

            string Feedback = txtEdtFeedback.Text;
            string Status = cmbEdtStatus.Text;
            string Transfer = cmbEdtTransfer.Text;

            string RefName = txtEdtRefComName.Text;
            string RefContact = txtEdtRefContact.Text;
            string BGC = txtEdtHowBGC.Text;

            BAL.Customers.Update_Lead_Generator(Id, Name, Contact, Location, Email, InterestedField, EnquiryType,
               Feedback, Status, Transfer, RefName, RefContact , BGC);
            populateLeadGeneration();
        }

        public void PopulateEnquiry(int type, string Location)
        {

            DataSet ds = BAL.Customers.GetEnquies(type, Location);
            if (type == 1)
            {
                spnMsg.InnerHtml = "List of Pending Enquries.";

                gvEnquiry.Columns[5].Visible = false;
                gvEnquiry.Columns[6].Visible = true;
                gvEnquiry.Columns[7].Visible = true;
                gvEnquiry.Columns[8].Visible = false;

            }
            else if (type == 0)
            {
                spnMsg.InnerHtml = "List of Unsolved Enquries.";
                gvEnquiry.Columns[5].Visible = true;
                gvEnquiry.Columns[6].Visible = false;
                gvEnquiry.Columns[7].Visible = false;
                gvEnquiry.Columns[8].Visible = true;
            }
            else
            {
                spnMsg.InnerHtml = "List of Success Enquires";
                gvEnquiry.Columns[5].Visible = false;
                gvEnquiry.Columns[6].Visible = false;
                gvEnquiry.Columns[7].Visible = false;
                gvEnquiry.Columns[8].Visible = false;
            } gvEnquiry.DataSource = ds;
            gvEnquiry.DataBind();

        }

        protected void gdCandidateProfile_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button SubmitButton = (Button)e.Row.FindControl("btnSubmit");

            }
        }

        protected void btnSchedule_Click(object sender, EventArgs e)
        {
            Button btnsc = (Button)sender;
            GridViewRow row = (GridViewRow)btnsc.Parent.Parent;


            int Eid = Convert.ToInt32(gvEnquiry.DataKeys[row.RowIndex]["EnquiryID"]);
            lblEnquiryId.Text = Convert.ToString(Eid);
            //  ModalPopupExtender1.TargetControlID = "btnsc";
            ModalPopupExtender1.Show();
        }

        protected void btnSucess_Click(object sender, EventArgs e)
        {
            Button btnsc = (Button)sender;
            GridViewRow row = (GridViewRow)btnsc.Parent.Parent;
            int Eid = Convert.ToInt32(gvEnquiry.DataKeys[row.RowIndex]["EnquiryID"]);
            //int EnquiryID = Convert.ToInt32( row.Cells[0].Text );

            BAL.Customers.UpdtChangetoSuccess(Eid, 2);




            PopulateEnquiry(1, Session["Location"].ToString());
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DateTime SchdeuleDate = Convert.ToDateTime(txtScheule.Text);
            int EnquiryID = Convert.ToInt32(lblEnquiryId.Text);

            BAL.Customers.UpdateEnquiry(SchdeuleDate, EnquiryID, 1);
            PopulateEnquiry(0, Session["Location"].ToString());
        }

        protected void btnTodaySchedule_Click(object sender, EventArgs e)
        {
            DataSet ds = BAL.Customers.GetEnquies(3, Session["Location"].ToString());

            spnMsg.InnerHtml = "List of Today's Schdeul";
            gvEnquiry.Columns[5].Visible = false;
            gvEnquiry.Columns[6].Visible = true;
            gvEnquiry.Columns[7].Visible = false;
            gvEnquiry.Columns[8].Visible = false;
            gvEnquiry.DataSource = ds;
            gvEnquiry.DataBind();

        }

        protected void btnTransfer_Click(object sender, EventArgs e)
        {
            Button btnsc = (Button)sender;
            GridViewRow row = (GridViewRow)btnsc.Parent.Parent;
            int Eid = Convert.ToInt32(gvEnquiry.DataKeys[row.RowIndex]["EnquiryID"]);
            lblEnqID.Text = Convert.ToString(Eid);
            ModalPopupExtender2.Show();
        }

        protected void lnkPending_Click(object sender, EventArgs e)
        {
            PopulateEnquiry(1, Session["Location"].ToString());
        }

        protected void lnkSolved_Click(object sender, EventArgs e)
        {
            PopulateEnquiry(2,Session["Location"].ToString());


        }

        protected void lnkUnsolved_Click(object sender, EventArgs e)
        {
            PopulateEnquiry(0, Session["Location"].ToString());
        }

        protected void lnkEnquiry_Click(object sender, EventArgs e)
        {
            LinkButton lnkEnquiry = (LinkButton)sender;
        }

        protected void ModalPopupExtender2_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BAL.Customers.PopulateLocation(ref ddlLocation1);
            }

        }

        protected void btntransfer_Click(object sender, EventArgs e)
        {

            string AssingLoc = ddlLocation1.SelectedValue;
            int Eid = Convert.ToInt32(lblEnqID.Text);
            BAL.Customers.updateAssgnLoc(AssingLoc, Eid);
            PopulateEnquiry(0, Session["Location"].ToString());
        }


    }
}